package mx.com.mindbits.bandpay.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import mx.com.mindbits.bandpay.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.View;

public class LayoutToImg {
	
	public void turnToImage(Bitmap bmpImg, Context mContext){
		View v = new View(mContext);
		v.findViewById(R.layout.activity_ticket);						
	}
	
	 public static void saveSignature(Bitmap image) {		    
	    	//Bitmap image = signature.getImage();		 	
			File sd = Environment.getExternalStorageDirectory();
			System.out.println(sd.toString());
			File fichero = new File(sd, "ticket.jpg");

			try {
				if (sd.canWrite()) {
	            	fichero.createNewFile();
					OutputStream os = new FileOutputStream(fichero);
					image.compress(Bitmap.CompressFormat.JPEG, ConstantsValues.QUALITY_COMPRESS, os);
					os.flush();
					os.close();
	            }
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }
	
	public static Bitmap getBitmapFromView(View view) {
		int w = 0;
		int h = 0;
		
		w = view.getWidth();
		h = view.getHeight();
		
		if (view.getWidth() <= 0 && view.getHeight() <= 0){
			w = 75;
			h = 150;
		}
	    Bitmap returnedBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
	    Canvas canvas = new Canvas(returnedBitmap);
	    Drawable bgDrawable =view.getBackground();
	    if (bgDrawable!=null) 
	        bgDrawable.draw(canvas);
	    else 
	        canvas.drawColor(Color.WHITE);
	    view.draw(canvas);
	    return returnedBitmap;
	}
	

}
